<?php

namespace Admin\Controllers;

class LoginController extends AuthController {

    public function loginAction() {
        $this->view->disableLevel(
                \Phalcon\Mvc\View::LEVEL_MAIN_LAYOUT
        );
        $this->view->pick('login/login');
    }

    public function ajaxLoginAction() {
        if (empty($_POST['username']))
            E('用户名不能为空');
        if (empty($_POST['password']))
            E('密码不能为空');
        $adminUser = service('Admin/AdminUserService')->login($_POST['username'], $_POST['password']);
        $userAuthConfig = $this->config->userAuthConfig->toArray();
        $this->session->set($userAuthConfig['USER_AUTH_KEY'], $adminUser['adminId']);
        $this->session->set("AdminUser", $adminUser);
        $this->session->set("realName", $adminUser['realName']);
        $this->session->set("img", $adminUser['img']);
        $this->session->set("roleId", $adminUser['roleId']);
        $this->ajaxReturn($adminUser, "登录成功");
    }

    public function ajaxLogoutAction() {
        $this->session->destroy();
        $this->ajaxReturn("", "退出成功");
    }

}
